﻿
@{ ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutList.cshtml"; }

<!--模糊搜索区域-->
<div class="layui-row">
    <form class="layui-form layui-col-md12 ok-search">
        <div class="layui-input-inline">
            <input class="layui-input" placeholder="日期范围" autocomplete="off" name="StartEndDate" id="StartEndDate">
        </div>
        @Html.SearchBtnHtml("查询")
        @Html.ResetBtnHtml()
    </form>
</div>
<!--数据表格-->
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
<script>
    layui.use(["table", "form", "laydate", "okLayer", "okUtils"], function () {
        let table = layui.table;
        let form = layui.form;
        let laydate = layui.laydate;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;
        let $ = layui.$;
        laydate.render({
            elem: '#StartEndDate'
            , range: '~'
        });

        let AllTable = table.render({
            elem: "#tableId",
            url: "/Shop/GoodsSet/List",
            limit: 10,
            page: true,
            toolbar: "#toolbarTpl",
            size: "sm",
            cols: [[
                { type: "checkbox" },
                { field: "Id", title: "ID", width: 80, sort: false },
                { field: "Mid", title: "物料表ID", width: 100 },
                { field: "Material_id", title: "物料ID", width: 100 },
                { field: "Title", title: "标题", width: 100 },
                { field: "ShortTitle", title: "短标题", width: 100 },
                { field: "LevelOneCategoryName", title: "一级类目", width: 100},
                { field: "ItemId", title: "宝贝ID", width: 100 },
                { field: "CouponTotalCount", title: "优惠券总量", width: 100 },
                { field: "CouponRemainCount", title: "优惠券剩余", width: 100 },
                { field: "ReservePrice", title: "一口价", width: 100 },
                { field: "ZkFinalPrice", title: "折扣价", width: 100 },
                { field: "CouponAmount", title: "优惠券金额", width: 100 },
                { field: "UserType", title: "商家类型", width: 100, templet: '#UserTypeTpl' },
                { field: "Volume", title: "销量", width: 100 },
                { field: "ShopTitle", title: "店铺名称", width: 80 },
                { field: "CouponShareUrl", title: "二合一", width: 100 },
                { field: "ClickUrl", title: "推广链", width: 100 },
                { field: "CreateTime", title: "录入时间", width: 100, templet: '<span>{{showDate(d.CreateTime)}}<span>' },
                { field: "UpdateTime", title: "上次更新时间", width: 160, templet: '<span>{{showDate(d.UpdateTime)}}<span>' },
                { title: "操作", width: 230, align: "center", fixed: "right", templet: "#operationTpl"}
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        form.on("submit(search)", function (data) {
            AllTable.reload({
                where: data.field,
                page: { curr: 1 }
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "batchDel":
                    batchDel();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.Id);//field Id 和 数据库表字段 Id 要一致
                    break;
                case "del":
                    del(data.Id);
                    break;
                case "pwdReset":
                    pwdReset(data.Id);
                    break;

                case "RefreshItemData":
                    RefreshItemData(data.Id);
                    break;
            }
        });

        function add() {
            okLayer.open("添加用户", "/Shop/MaterialSet/Add", "100%", "100%", null, null);
        }

        function batchDel() {
            okLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                let idsStr = okUtils.tableBatchCheck(table);
                if (idsStr) {
                    okUtils.ajax("/Shop/MaterialSet/BatchDel", "get", { idsStr: idsStr }, true).done(function (response) {
                        okUtils.tableSuccessMsg(response.message);
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function edit(id) {
            okLayer.open("编辑", "/Shop/GoodsSet/Edit/" + id, "100%", "100%", null, null);
        }

        function del(id) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax("/Shop/MaterialSet/Delete", "get", { id: id }, true).done(function (response) {
                    okUtils.tableSuccessMsg(response.message);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }

        function RefreshItemData(id) {
            okLayer.confirm("确定要更新吗？", function () {
                okUtils.ajax("/Shop/MaterialSet/RefreshData", "get", { id: id }, true).done(function (response) {
                    okUtils.tableSuccessMsg(response.message);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }

        $('#RefreshData').on('click', function () {
            okLayer.confirm("确定要删除全部超期数据（7日前）么？", function () {
                okUtils.ajax("/Shop/GoodsSet/OverData", "get", {}, true).done(function (response) {
                    okUtils.tableSuccessMsg(response.message);
                    //没开启分页，没确定按钮，手动刷新
                    setTimeout(function () {
                        window.location.reload();
                    }, 1500);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        });

    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    @Html.TopToolBarHtml(ViewData["TopButtonList"])
    <a class='layui-btn layui-btn-sm ' href='javascript:;' lay-event='RefreshData' id='RefreshData'><i class='ok-icon'></i>删除超期数据</a>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    @Html.RightToolBarHtml(ViewData["RightButtonList"])
    
</script>
<!-- 启用|停用模板 -->
<script type="text/html" id="enabledMarkTpl">
    {{#  if(!d.EnabledMark){ }}
    <span class="layui-btn layui-btn-normal layui-btn-xs">已启用</span>
    {{#  } else{ }}
    <span class="layui-btn layui-btn-warm layui-btn-xs">已停用</span>
    {{#  } }}
</script>
<!-- 模板 -->
<script type="text/html" id="UserTypeTpl">
    {{#  if(d.UserType==0){ }}
    <span>集市</span>
    {{#  } else{ }}
    <span>天猫</span>
    {{#  } }}
</script>
